import axios from 'axios';
import NProgress from 'nprogress';
import 'nprogress/nprogress'
import nprogress from 'nprogress';
import store from '@/store';
import "nprogress/nprogress.css";

NProgress.configure({ showSpinner: false })

const request = axios.create({
    baseURL: '/api', //基础路径
    timeout: 15000  //连接请求超时时间
})
// 请求拦截器
request.interceptors.request.use((config) => {
    NProgress.start()
    // 请求头添加字段
    if (store.state.detail.uuid_token) {
        config.headers.userTempId = store.state.detail.uuid_token
    }
    // 携带token带给服务器
    if (store.state.user.token) {
        config.headers.token = store.state.user.token
    }
    return config
})
// 响应拦截器
request.interceptors.response.use((response) => {
    nprogress.done()
    return response.data
}, (error) => {
    NProgress.done()

    //统一处理错误
    alert(`请求出错：${error.message || '未知错误'}`)

    return Promise.reject(error)
})


export default request